package com.aiit.service.impl;

import com.aiit.dto.BookingDTO;
import com.aiit.dto.UserBookingDTO;
import com.aiit.entity.Booking;
import com.aiit.entity.UserBooking;
import com.aiit.mapper.BookingMapper;
import com.aiit.service.BookingService;
import com.aiit.vo.BookingVO;
import com.github.pagehelper.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BookingServiceImpl implements BookingService {


    @Autowired
    private BookingMapper bookingMapper;


    public BookingVO select(String date) {
        // 获取一些数据的逻辑
        List<Booking> booking = bookingMapper.select(date);

        // 创建并返回一个 BookingVO 对象
        BookingVO bookingVO = new BookingVO();

        // 假设我们需要从每个 Booking 对象中提取某些信息并汇总到 BookingVO 中
        for (Booking bookings : booking) {
            // 例如，将每个 Booking 的某个属性累加到 BookingVO 的某个属性中
            bookingVO.setStatus(bookings.getStatus());
            bookingVO.setSeat_number(bookings.getSeatNumber());
        }

        // 设置其他属性
        bookingVO.setBooking(booking);

        return bookingVO;
    }


    public void update(BookingDTO bookingDTO) {
        Booking booking = new Booking();
        BeanUtils.copyProperties(bookingDTO, booking);

        booking.setUpdateTime(LocalDateTime.now());

        bookingMapper.update(booking);
        //   bookingMapper.updateUserBooking(booking);
    }

    @Override
    public List<Booking> list(Booking booking) {
        List<Booking> list = bookingMapper.list(booking);
        return list;
    }

    public int updateBookingsWithOldSessionTime() {

        LocalDateTime fiveMinutesAgo = LocalDateTime.now().minusMinutes(5);
        return bookingMapper.updateBookingsBySessionTime(fiveMinutesAgo);
    }

    @Override
    public void savebookings(UserBookingDTO userBookingDTO) {
        System.out.println("当前线程的id:" + Thread.currentThread().getId());
        UserBooking userBooking = new UserBooking();

        //对象属性拷贝
        BeanUtils.copyProperties(userBookingDTO, userBooking);

        userBooking.setCreateTime(LocalDateTime.now());
        userBooking.setUpdateTime(LocalDateTime.now());

        bookingMapper.insertUserBooking(userBooking);
        bookingMapper.reduce(userBooking.getSessionTime());

    }

    @Override
    public int updateUserBookingWithOldSessionTime() {
        LocalDateTime fiveMinutesAgo = LocalDateTime.now().minusMinutes(5);
        return bookingMapper.updateUserBookingBySessionTime(fiveMinutesAgo);
    }

    @Override
    public int updateUserBookingStatus() {

        return bookingMapper.updateUserBookingStatus();
    }

    @Override
    public Page<UserBookingDTO> userBooking(UserBookingDTO userBookingDTO) {
        Page<UserBookingDTO> orders = bookingMapper.getByUserBooking(userBookingDTO);

        return orders;
    }

}
